//
//  ViewController.m
//  九宫格
//
//  Created by song on 15/1/5.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "SJOApp.h"


@interface ViewController ()

@property(nonatomic , strong) NSArray *imagedata;

@property(nonatomic ,strong) NSArray *apps;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    int pcount = 3;
    //这个应用的长宽
    CGFloat appW = 80;
    CGFloat appH =80;
    //t图片的长宽
    CGFloat imgW = 40;
    CGFloat imgH = 40;
    //标签的高度
    CGFloat lableH = 20;
    //按钮的高度
    CGFloat btnH = 20;
    // 每个应用的 间隙的宽度
    CGFloat appMargin = (self.view.frame.size.width - pcount * appW )/ (pcount+1);
    
    
    for(int i =0 ;i<self.apps.count;i++)
    {
        int  x =  i% pcount ;   // 这个应用再第几列
        int y = i /pcount ;     // 应用再第几行
        
        NSString *icon = ((SJOApp *)self.apps[i]).icon;
        NSString *content = ((SJOApp *)self.apps[i]).name;
        
        CGFloat xpos =  appMargin * (x  +1)  + x * appW; // 计算 x 的坐标的 m+h|m+h|m+h|m+h  就知道了
        CGFloat ypos =  appMargin * (y  +1)  +y * appH;
        
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(xpos, ypos, appW, appH);

        
        [self.view addSubview:view];
        
        
        int imgX = (appW - imgW)*0.5;
        UIImageView *image =[[UIImageView alloc] init];
        image.frame = CGRectMake(imgX, 0, imgW, imgH);
        image.image = [UIImage imageNamed:icon];        //设置图片
        
        UILabel *lable = [[UILabel alloc] init];
        lable.frame =  CGRectMake(0, imgH, appW, lableH);

        lable.text =content;
        lable.font = [UIFont systemFontOfSize:13];      //设置字体大小
        lable.textAlignment = NSTextAlignmentCenter;    //文字居中对齐
        
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(0 , imgH+lableH , appW, btnH);
        
        //设置再两种状态下面按钮的图片
        UIImage *normalimag = [UIImage imageNamed:@"buttongreen"];
        [btn setTitle:@"下载" forState:UIControlStateNormal];
        [btn setBackgroundImage:normalimag forState:UIControlStateNormal];
        
        UIImage *hightimag = [UIImage imageNamed:@"buttongreen_highlighted"];
        [btn setBackgroundImage:hightimag forState:UIControlStateHighlighted];
        
        btn.titleLabel.font =[UIFont systemFontOfSize:13];

        
        
        [view addSubview:btn];
        [view addSubview:lable];
        [view addSubview:image];
        
        
        
        
    }
    
    
    
    
    
}

-(NSArray *)apps
{
    if(_apps==nil)
    {
        NSMutableArray *appdata = [[NSMutableArray alloc]initWithCapacity:0];
        NSBundle *mainbundle = [NSBundle mainBundle];
        NSString *ppath =[mainbundle pathForResource:@"imagedata" ofType:@"plist"];
        NSArray *resouces =[NSArray arrayWithContentsOfFile:ppath];
        for(NSDictionary *dic in resouces)
        {
            SJOApp *app = [[SJOApp alloc]init];
            app.icon  = dic[@"icon"];
            app.name = dic[@"name"];
            [appdata addObject:app];
            
        }
        _apps = appdata;
    }
    return _apps;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
